﻿using System;
using System.Windows.Forms;
using Microsoft.Office.Interop.Excel;

namespace VSTODemo {
    public partial class SqlForm : Form {
        public SqlForm() {
            InitializeComponent();
        }

        private void btnConfirm_Click(object sender, EventArgs e) {
            System.Data.DataTable values = null;
            Worksheet sheet = null;

            try {
                values = EmployeeService.GetDataFromSql(txtSqlStatement.Text.Trim());
            }catch(Exception ex) {
                MessageBox.Show(ex.Message);
                return;
            }
            
                       
            if (newSheetRadioButton.Checked) { // 在新的工作表显示 
                sheet = ThisAddIn.ExcelApp.Worksheets.Add();                
            }
            else { // 在当前工作表显示
                sheet = ThisAddIn.ExcelApp.ActiveSheet;
                sheet.Cells.ClearContents();
            }

            ExcelUtils.CopyFromDataTable(values, sheet);
        }
    }
}
